package sv.gob.mh.dgii.security;

import java.util.Collection;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.ldap.userdetails.LdapUserDetails;
import org.springframework.security.ldap.userdetails.LdapUserDetailsMapper;
import sv.gob.mh.dgii.common.Utils;

public class SISCOLdapUserDetailsMapper extends LdapUserDetailsMapper {

        private static final String DEFAULT_NIT = "00000000000000";
	private static final String DEFAULT_UNIDAD = "0000";
	private static final String DEFAULT_ADM_TRIB = "01";
	private static final String DEFAULT_UBICACION_FISICA = "Sin ubicacion";

        @Override
	public UserDetails mapUserFromContext(DirContextOperations ctx,
			String username, Collection<? extends GrantedAuthority> authorities) {

		SscUserDetailsImpl userDetails = new SscUserDetailsImpl(
				(LdapUserDetails) super.mapUserFromContext(ctx, username,
						authorities));

		userDetails.setEmail(((String) ctx.getObjectAttribute("mail")).trim());
		userDetails.setDisplayName((String) ctx
				.getObjectAttribute("displayname"));
		
		String nomInstitucion = (String) ctx.getObjectAttribute("nomInstitucion");
                String emailPi = (String) ctx.getObjectAttribute("email_pi");
                String emailServ = (String) ctx.getObjectAttribute("email_serv");
                String ctrlchgpwd = (String)ctx.getObjectAttribute("ctrlchgpwd");
                String adminTri = (String) ctx.getObjectAttribute("adminTrib");
                String banderas = (String) ctx.getObjectAttribute("admintrib");
		String nitRepresentante = (String) ctx.getObjectAttribute("representante");
                String nit = (String) ctx.getObjectAttribute("nit");
		String ubicacionFisica = (String) ctx.getObjectAttribute("ubicaFisica");		
		String cunidad = (String) ctx.getObjectAttribute("unidad");
                String dui = (String) ctx.getObjectAttribute("dui"); 
                
                if(!Utils.isNullOrEmpty(banderas)){
                    userDetails.setBanderas(banderas);
                }
                if(!Utils.isNullOrEmpty(ctrlchgpwd)){
                        userDetails.setCtrlchgpwd("1".equals(ctrlchgpwd)?true:false);
                }                
                if(!Utils.isNullOrEmpty(emailPi)){
                        userDetails.setEmail(emailPi.trim());
                        userDetails.setEmailPi(emailPi.trim());			
		}
                if(!Utils.isNullOrEmpty(emailServ)){			
                        userDetails.setEmailServ(emailServ.trim());
		}
		if(!Utils.isNullOrEmpty(nomInstitucion)){
			userDetails.setNomInstitucion( nomInstitucion.trim() );
		}
		if(!Utils.isNullOrEmpty(nitRepresentante)){
			userDetails.setNitRepresentante( nitRepresentante.trim() );
		}
		if (!Utils.isNullOrEmpty(nit)) {
			userDetails.setNit(nit);
		} else {
			userDetails.setNit(DEFAULT_NIT);
		}		
		if (!Utils.isNullOrEmpty(dui)) {
			userDetails.setDui(dui);
		} else {
			userDetails.setDui("N/A");
		}
		if (!Utils.isNullOrEmpty(ubicacionFisica)) {
			userDetails.setUbicacionFisica(ubicacionFisica);
		} else {
			userDetails.setUbicacionFisica(DEFAULT_UBICACION_FISICA);
		}

		if (!Utils.isNullOrEmpty(adminTri)) {
			userDetails.setCadmTrib(adminTri);
		} else {
			userDetails.setCadmTrib(DEFAULT_ADM_TRIB);
		}

               
		return userDetails;
	}
}